﻿#include "imagecarousel.h"

#include <QLabel>

ImageCarousel::ImageCarousel(QWidget *parent): QGraphicsView(parent)
{
    pixmapItem = new QLabel(parent);

    width = parent->width();
    height = parent->height();

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &ImageCarousel::showNextImage);
}

void ImageCarousel::setImageList(const QList<QPixmap> &images) {
    this->images = images;
    if (!images.isEmpty()) {
        currentIndex = 0;
        QPixmap img = ImageTools::getRoundedPixmap(images.at(0),16,width,height);
        pixmapItem->setPixmap(images.at(0));
    }
}

void ImageCarousel::startCarousel(int interval) { // 默认间隔3秒
    if (!images.isEmpty()) {
        timer->start(interval);
    }
}

void ImageCarousel::stopCarousel() {
    timer->stop();
}

void ImageCarousel::showNextImage() {
    currentIndex++;
    if(currentIndex ==  images.size())  currentIndex = 0;

    QPixmap img = ImageTools::getRoundedPixmap(images.at(currentIndex),16,width,height);
    pixmapItem->setPixmap(img);
    qDebug() << "Show image" << currentIndex;
}


